package org.baiyun.kaka.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;
import org.baiyun.kaka.entity.Vip;
import org.baiyun.kaka.service.IVipService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class VipAction implements ModelDriven<Vip> {
	
	//这里不需要@Autowired, 因为struts2框架已经做了，只i需要我们对servic去设置get和set方法
	private IVipService vipService;
	
	private List<Vip> list;
	
	
	private Vip vip = new Vip(); //定义了一个vip用来接收数据库查询出来的结果
	
	
	public Vip getVip() {
		return vip;
	}

	public void setVip(Vip vip) {
		this.vip = vip;
	}


	public IVipService getVipService() {
		return vipService;
	}

	public void setVipService(IVipService vipService) {
		this.vipService = vipService;
	}
	

	public List<Vip> getList() {
		return list;
	}

	public void setList(List<Vip> list) {
		this.list = list;
	}

	//用来查询vip列表的方法
	public String list() //查询全部数据
	{
			
		list = vipService.findAll();
			
		return "success";
	}
	
	//用来删除vip的方法
	public String delete() 
	{		
		boolean ret=vipService.delete(vip.getId());
			
		return "success";
	}
	
	
	public String update() {  //显示一个修改页面
		
		HttpServletRequest  request = (HttpServletRequest)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
		
		String m = request.getParameter("m");
		
	    if (m!=null && m!="" && m.equals("d")) {
	    	vip = vipService.get(vip.getId());
	    	ActionContext.getContext().put("oper", 1); //往值栈中添加变量，并设置值（目的是希望页面根据这个值来决定向哪个action去提交）
	    	return "display";
	    }else {
	    	vipService.update(vip);
	    	return "success";
	    }					
		
	}

	public String add() {  //显示一个添加页面
		
		HttpServletRequest  request = (HttpServletRequest)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
		
		String test = "";
		
		String m = request.getParameter("m"); 
		
	    if (m!=null && m!="" && m.equals("d")) {
	    	ActionContext.getContext().put("oper", 2);//往值栈中添加变量，并设置值（目的是希望页面根据这个值来决定向哪个action去提交）
	    	return "display";
	    }else {
	    	vipService.save(vip);
	    	return "success";
	    }					
		
	}
	
	@Override
	public Vip getModel() {
		// TODO 自动生成的方法存根
		return vip;
	}

}
